<template>
	<div>
		<!--引用表格-->
		<BasicTable @register="registerTable" :rowSelection="rowSelection">
			<!--插槽:table标题-->
			<template #tableTitle>
				<a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
				<a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
				<j-upload-button type="primary" preIcon="ant-design:import-outlined"
					@click="onImportXls">导入</j-upload-button>
				<a-dropdown v-if="selectedRowKeys.length > 0">
					<template #overlay>
						<a-menu>
							<a-menu-item key="1" @click="batchHandleDelete">
								<Icon icon="ant-design:delete-outlined"></Icon>
								删除
							</a-menu-item>
						</a-menu>
					</template>
					<a-button>批量操作
						<Icon icon="mdi:chevron-down"></Icon>
					</a-button>
				</a-dropdown>
			</template>
			<!--操作栏 :dropDownActions="getDropDownAction(record)"-->
			<template #action="{ record }">
				<TableAction :actions="getTableAction(record)" />
			</template>
			<!--字段回显插槽-->
			<template v-slot:bodyCell="{ column, record, index, text }">
			</template>
		</BasicTable>
		<a-tabs defaultActiveKey="1" style="margin-left: 10px;">
			<a-tab-pane tab="出发日期" key="1">
				<bsTravelgroupMaintuortravellinerecordMaxGroupListTab />
			</a-tab-pane>
		</a-tabs>
		<!-- 表单区域 -->
		<BsTravelgroupTravelistgroupModal @register="registerModal" @success="handleSuccess">
		</BsTravelgroupTravelistgroupModal>
		<selectHomePageTypeModule @register="registerSettingsModal" @success="handleSuccess">
		</selectHomePageTypeModule>
		<EditGroupDrawer @register="registerDrawer" @success="handleSuccess">
		</EditGroupDrawer>
		<QrImgModal @register="registerQrImgModal" @success="handleSuccess">
		</QrImgModal>
	</div>
</template>

<script lang="ts" name="journey-bsTravelgroupTravelistgroup" setup>
	import { ref, computed, unref, provide } from 'vue';
	import { BasicTable, useTable, TableAction } from '/@/components/Table';
	import { useModal } from '/@/components/Modal';
	import { useListPage } from '/@/hooks/system/useListPage'
	import { getHomeTypeIdByGid } from './components/drawer/index.api';
	import BsTravelgroupTravelistgroupModal from './components/BsTravelgroupTravelistgroupModal.vue'
	import selectHomePageTypeModule from '../../../module/jounery/selectHomePageTypeModule.vue'
	import QrImgModal from '../../../module/qrCodeImg.vue'
	import bsTravelgroupMaintuortravellinerecordMaxGroupListTab from './bsTravelgroupMaintuortravellinerecordMaxGroupListTab.vue'
	import { columns, searchFormSchema } from './BsTravelgroupTravelistgroup.data';
	import EditGroupDrawer from './components/drawer/index.vue'
	import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, getMsgsByGroup } from './BsTravelgroupTravelistgroup.api';
	import { downloadFile } from '/@/utils/common/renderUtils';
	import { useUserStore } from '/@/store/modules/user';
	import { useDrawer } from '/@/components/Drawer';
	const checkedKeys = ref<Array<string | number>>([]);
	const userStore = useUserStore();
	const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
	//注册model
	const [registerSettingsModal, { openModal: openSettingsModal }] = useModal();
	const [registerQrImgModal, { openModal: openQrImgModal }] = useModal();
	const [registerModal, { openModal }] = useModal();
	//注册table数据
	const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
		tableProps: {
			title: '大团行程组',
			api: list,
			columns,
			canResize: false,
			rowSelection: { type: 'radio' },
			formConfig: {

				//labelWidth: 120,
				schemas: searchFormSchema,
				autoSubmitOnEnter: true,
				showAdvancedButton: true,
				fieldMapToNumber: [
				],
				fieldMapToTime: [
				],
			},
			actionColumn: {
				width: 160,
				fixed: 'left'
			},
		},
		exportConfig: {
			name: "大团行程组",
			url: getExportUrl,
		},
		importConfig: {
			url: getImportUrl,
			success: handleSuccess
		},
	})

	const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext
	const mainId = computed(() => (unref(selectedRowKeys).length > 0 ? unref(selectedRowKeys)[0] : ''));
	//下发 mainId,子组件接收
	provide('mainId', mainId);
	/**
	 * 新增事件
	 */
	async function handleAdd() {

		let o = await getMsgsByGroup();
		let record = {
			retrofitRule: o.retrofitRule,
			mustSee: o.mustSee
		}

		openDrawer(true, {
			record,
			isUpdate: false,
			showFooter: true,
			isCopy: false
		});

		/* openModal(true, {
			isUpdate: false,
			showFooter: true,
		}); */
	}
	/**
	 * 复制
	 */
	async function handleCopy(record : Recordable) {
		let titleGroup = JSON.parse(record.titlegroup)
		record.secondTitle = titleGroup.secondTitle
		record.mainTitle = titleGroup.mainTitle
		record.images2 = JSON.parse(record.images).join(',')
		let pt = JSON.parse(record.startpoint);

		record.cityIds = pt.point[0].cityId
		record.jDays = pt.point[0].playDays
		record.pickUpIds = pt.point[0].upCatPointId
		record.travelLineId = pt.point[0].travelLineId
		record.virtualUserNum = pt.point[0].virtualUserNum
		record.maxUserNum = pt.point[0].maxUserNum || record.maxUserNum

		if (pt.alternatePickUpPoint) {
			record.alternatePickUpPoint = pt.alternatePickUpPoint[0].upCatPointId
		}
		let extinfor = JSON.parse(record.extinfor)
		record.groupUserSize = extinfor.groupUserSize
		record.tagsTitle = extinfor.tagsTitle
		record.msg = extinfor.msg
		record.destination = extinfor.destination
		record.isUpdate = true
		record.crossedPrice = extinfor.crossedPrice
		record.keyCode = extinfor.keyCode || ''
		//类别选择
		record.homeType = extinfor.homeType
		record.xjSelect = extinfor.xjSelect
		record.startdateStr = extinfor.startdateStr
		record.enddateStr = extinfor.enddateStr
		record.sjSelect = extinfor.sjSelect
		record.retrofitRule = extinfor.retrofitRule
		record.isEarly = extinfor.isEarly
		record.earlyBirdPrice = extinfor.earlyBirdPrice
		record.travelcalendar = extinfor.travelcalendar
		record.stepprice = extinfor.stepprice
		record.mustSee = extinfor.mustSee
		record.payPrice = extinfor.payPrice
		record.userSelectDay = extinfor.userSelectDay
		//上下架-定时上下架，手动上下架

		//早鸟价

		//用户可选日期
		//确定出发日期
		//成团阶梯价格
		//支付优惠价格
		//退改规则
		//行前必看


		openDrawer(true, {
			record,
			isUpdate: false,
			showFooter: true,
			isCopy: true
		});
	}


	/**
	 * 编辑事件
	 */
	async function handleEdit(record : Recordable) {


		let titleGroup = JSON.parse(record.titlegroup)
		record.secondTitle = titleGroup.secondTitle
		record.mainTitle = titleGroup.mainTitle
		record.images2 = JSON.parse(record.images).join(',')
		let pt = JSON.parse(record.startpoint);

		record.cityIds = pt.point[0].cityId
		record.jDays = pt.point[0].playDays
		record.pickUpIds = pt.point[0].upCatPointId
		record.travelLineId = pt.point[0].travelLineId
		record.virtualUserNum = pt.point[0].virtualUserNum
		record.maxUserNum = pt.point[0].maxUserNum || record.maxUserNum

		if (pt.alternatePickUpPoint) {
			record.alternatePickUpPoint = pt.alternatePickUpPoint[0].upCatPointId
		}
		let extinfor = JSON.parse(record.extinfor)
		record.groupUserSize = extinfor.groupUserSize
		record.tagsTitle = extinfor.tagsTitle
		record.msg = extinfor.msg
		record.destination = extinfor.destination
		record.isUpdate = true
		record.crossedPrice = extinfor.crossedPrice
		record.keyCode = extinfor.keyCode || ''
		//类别选择
		record.homeType = extinfor.homeType
		record.xjSelect = extinfor.xjSelect
		record.startdateStr = extinfor.startdateStr
		record.enddateStr = extinfor.enddateStr
		record.sjSelect = extinfor.sjSelect
		record.retrofitRule = extinfor.retrofitRule
		record.isEarly = extinfor.isEarly
		record.earlyBirdPrice = extinfor.earlyBirdPrice
		record.travelcalendar = extinfor.travelcalendar
		record.stepprice = extinfor.stepprice
		record.mustSee = extinfor.mustSee
		record.payPrice = extinfor.payPrice
		record.userSelectDay = extinfor.userSelectDay
		//上下架-定时上下架，手动上下架

		//早鸟价

		//用户可选日期
		//确定出发日期
		//成团阶梯价格
		//支付优惠价格
		//退改规则
		//行前必看


		openDrawer(true, {
			record,
			isUpdate: true,
			showFooter: true,
			isCopy: false
		});

		/* 	openModal(true, {
				record,
				isUpdate: true,
				showFooter: true,
			}); */
	}
	/**
	 * 详情
	*/
	function handleDetail(record : Recordable) {


		openModal(true, {
			record,
			isUpdate: true,
			showFooter: false,
		});
	}
	/**
	 * 删除事件
	 */
	async function handleDelete(record) {
		await deleteOne({ id: record.id }, handleSuccess);
	}
	/**
	 * 批量删除事件
	 */
	async function batchHandleDelete() {
		await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
	}


	function handleTypeSet(record) {

		openSettingsModal(true, {
			id: record.id,
			isUpdate: true,
			showFooter: true,
		})
	}

	function handleTest(record) {
		openDrawer(true, {
			record,
			showFooter: true
		});
	}

	function handleShare(record : Recordable) {
		//生成小程序二维码和分享链接
		openQrImgModal(true, {
			isUpdate: true,
			record,
			showFooter: false
		})

	}


	/**
	 * 成功回调
	 */
	function handleSuccess() {
		(selectedRowKeys.value = []) && reload();
	}
	/**
	   * 操作栏
	   */
	function getTableAction(record) {
		return [
			{
				label: '编辑',
				onClick: handleEdit.bind(null, record),
			},
			{
				label: '推广',
				onClick: handleShare.bind(null, record),
			},
			{
				label: '复制',
				onClick: handleCopy.bind(null, record),
			}/* ,
			{
				label: '测试',
				onClick: handleTest.bind(null, record),
			} */
		]
	}
	/**
	   * 下拉操作栏
	   */
	function getDropDownAction(record) {
		return [
			{
				label: '详情',
				onClick: handleDetail.bind(null, record),
			}, {
				label: '删除',
				popConfirm: {
					title: '是否确认删除',
					confirm: handleDelete.bind(null, record),
					placement: 'topLeft',
				}
			},/* {
				label: '推荐到首页',
				onClick: handleTypeSet.bind(null, record),
			}, */
		]
	}
</script>

<style scoped>

</style>